Page({
  data: {
    books: [],
    fullPath: "",
    // ------------------
    keyword: "", // 关键字
    history: wx.getStorageSync("history") || [], // 历史搜索数组
  },
  //------------------4. 搜索历史
  searchByKeyword() {
    // 4.1 解构数据
    const { keyword, history } = this.data;
    // 4.2 把关键字放入数组
    if (!keyword.trim()) {
      return wx.showToast({
        title: "搜索内容不能为空",
        icon: "none",
      });
    }
    history.unshift(keyword);
    // 4.3 驱动视图更新
    this.setData(
      {
        history,
        keyword: "",
      },
      () => {
        // 4.4 存储到本地
        wx.setStorageSync("history", history);
      }
    );
  },
  // 清空历史搜索
  clearHistory() {
    this.setData(
      {
        history: [],
      },
      () => {
        wx.removeStorageSync("history");
      }
    );
  },
  //-------------------- 1. 获取图书信息
  getBooks() {
    // 1.1 加载提示
    wx.showLoading({
      title: "正在拼命加载中...",
      mask: true,
    });

    // 1.2 发起网络请求
    wx.request({
      url: "https://hmajax.itheima.net/api/books",
      data: {
        creator: "yjh",
      },
      method: "GET",
      success: (res) => {
        this.setData(
          {
            books: res.data.data,
          },
          () => {
            wx.showToast({
              title: "加载成功",
              icon: "success",
            });
          }
        );
      },
      fail(err) {
        console.log(err);
      },
      complete(res) {
        // 1.3 关闭提示
        wx.hideLoading();
      },
    });
  },

  // ------------------- 2. 本地存储
  // 2.1 同步
  setStorageData() {
    // 简单的, 引用的
    // wx.setStorageSync("token", "dwewdew323232dsad");
    wx.setStorageSync("user", { name: "张三", age: 18, sex: "男" });
  },
  getStorageData() {
    const token = wx.getStorageSync("token");
    wx.showToast({
      title: token,
      icon: "none",
    });
  },
  delStorageData() {
    wx.removeStorageSync("token");
  },
  clearStorageData() {
    wx.clearStorageSync();
  },

  // 2.2 异步
  async setStorageDataAsync() {
    // wx.setStorage({
    //   key: "TOKEN",
    //   data: "我是token",
    //   success(res) {
    //     console.log(res);
    //   },
    // });
    // 异步API如果支持promise, 则可以用async/await
    const res = await wx.setStorage({ key: "TOKEN", data: "我是token6666666" });
    console.log(res);
  },
  getStorageDataAsync() {
    wx.getStorage({
      key: "TOKEN",
      success(res) {
        console.log(res.data);
      },
    });
  },
  delStorageDataAsync() {
    wx.removeStorage({
      key: "TOKEN",
      success(res) {
        console.log(res);
      },
    });
  },
  clearStorageDataAsync() {
    wx.clearStorage({
      success(res) {
        console.log(res);
      },
    });
  },

  //---------------3.相册和拍照
  async chooseImage() {
    // 3.1 写法1
    /*
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["album"],
      sizeType: ["original"],
      success: (res) => {
        console.log(res);
        this.setData({
          fullPath: res.tempFiles[0].tempFilePath,
        });
      },
    });
    */

    // 3.2 promise写法
    const res = await wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["album"],
      sizeType: ["original"],
    });
    // console.log(res);
    this.setData({
      fullPath: res.tempFiles[0].tempFilePath,
    });
  },

  // ------------------
  // e: 相当于事件对象
  clickMe(e) {
    console.log(e);
  },
});
